//
//  ZZTaskDataEditVC.swift
//  QMXK
//
//  Created by jenkin on 2018/12/13.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskDataEditVC: ZZBaseTableVC {

    var dataDict:Dictionary<String, Any>!
    var editButton:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        
        self.data = self.dataDict["data"] as? Array<AnyObject>
        self.tableView.reloadData()
    }
    
    override func setUI() {
        super.setUI()
        
        
        self.tableView.frame = CGRect.init(x: 0, y: self.navH, width: self.screenW, height: self.screenH - self.navH)
        self.tableView.register(ZZTaskDataCell.classForCoder(), forCellReuseIdentifier: "Cell")
        //self.tableView.isEditing = true
        
        self.editButton = ZZViewUtil.shareViewUtil.createButton(title: "编辑", selectTitle: "完成", titleColor: ColorConst.shareColorConst.titleColor())
        self.view.addSubview(self.editButton)
        self.editButton.snp.makeConstraints { (make) in
            make.right.equalTo(-30)
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.top.equalTo(30)
        }
        self.editButton.addTarget(self, action: #selector(editAction(sender:)), for: UIControl.Event.touchUpInside)
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:ZZTaskDataCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ZZTaskDataCell
        
        let data:Dictionary<String, Any> = self.data![indexPath.row] as! Dictionary
        let open:CGFloat = data["open"] as!  CGFloat
        let finish:CGFloat = data["finish"] as! CGFloat
        let min:CGFloat = data["min"] as! CGFloat
        let max:CGFloat = data["max"] as! CGFloat
        
        let str:String = "open:" + ZZBaseUtil.shareUtil.getStrWithFloat(text: open) + " finish:" + ZZBaseUtil.shareUtil.getStrWithFloat(text: finish)  + "\n min:" + ZZBaseUtil.shareUtil.getStrWithFloat(text: min) + " max:" + ZZBaseUtil.shareUtil.getStrWithFloat(text: max)
        cell.titleLabel.text = str
        
        return cell;
    }
    
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete{
            self.data.remove(at: indexPath.row)
            self.dataDict["data"] = self.data
            ZZBaseUtil.shareUtil.saveTypeName(data: self.dataDict)
            tableView.reloadData()
        }
    }
    
    //是否可以移动
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let sourceData = self.data[sourceIndexPath.row]
        let desData = self.data[destinationIndexPath.row]
        
        self.data.replaceSubrange(Range.init(NSRange.init(location: sourceIndexPath.row, length: 1))!, with: [desData])
        self.data.replaceSubrange(Range.init(NSRange.init(location: destinationIndexPath.row, length: 1))!, with: [sourceData])
        self.dataDict["data"] = self.data
        ZZBaseUtil.shareUtil.saveTypeName(data: self.dataDict)
        tableView.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let taskVC:ZZTaskDataUpdateVC! = ZZTaskDataUpdateVC.init()
        taskVC.dataDict = self.dataDict
        taskVC.dataIndex = indexPath.row
        weak var weakSelf = self
        taskVC.dataSaveBlock = {(dataDict:Dictionary?) in
            weakSelf?.dataDict = dataDict
            weakSelf?.data = weakSelf?.dataDict["data"] as! Array<AnyObject>
            weakSelf?.tableView.reloadData()
        }
        self.navigationController?.pushViewController(taskVC, animated: true)
    }
    
    @objc func editAction(sender:UIButton){
        sender.isSelected = !sender.isSelected
        self.tableView.isEditing = sender.isSelected
    }
}
